package com.yanqu.road.server.manger.activity.area;

import com.yanqu.road.dao.impl.activity.entername.GameEnterNameConfigDaoImpl;
import com.yanqu.road.entity.activity.ActivityConfig;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.area.wase.AreaWashePkConfig;
import com.yanqu.road.entity.enums.eActivityConfigType;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.logic.config.ChannelConfig;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.area.AreaWashePkModule;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.activity.entername.EnterNameActivityConfig;
import com.yanqu.road.server.manger.activity.entername.EnterNameActivityMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.utils.string.StringUtils;

import java.util.List;
import java.util.Map;

/**
 * 跨域瓦舍地区pk
 */
public class AreaWasePkMgr extends TempMgr {

    private static ActivityInfo activityInfo;

    private static AreaWashePkConfig config;

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return true;
    }

    public static void reloadActivityData() {
        getLogger().info("reload Area Wase Pk Activity start");

        // 先检查有没有数据库连接配置
        String dbParam = ChannelConfig.CHANNEL_CENTER_DB_PARAM;
        if ("0".equals(dbParam)) {
            getLogger().info("[ ChannelConfig.CHANNEL_CENTER_DB_PARAM ] is zero!");
            return;
        }

        ActivityInfo tempActivityInfo;

        // 获取进行中的活动信息
        List<ActivityInfo> openActivityInfoList = NormalActivityMgr.getOpenActivityInfoList(eActivityType.AreaWashePkActivity.getValue());
        if (openActivityInfoList.isEmpty()) {
            getLogger().info("no Area Wase Pk Activity in show time");
            activityInfo = null;
            config = null;
            EnterNameActivityMgr.removeConfig(eActivityType.AreaWashePkActivity.getValue());
            return;
        } else {
            tempActivityInfo = openActivityInfoList.get(0);
            if(tempActivityInfo != null){
                getLogger().info("Area Wase Pk Activity {}", tempActivityInfo.getActivityId());
            }
        }

        if (tempActivityInfo != null) {

            // 加载配置
            EnterNameActivityConfig nameConfig = new EnterNameActivityConfig(tempActivityInfo);
            int tmpActivityId = tempActivityInfo.getActivityId();
            nameConfig.setMatchRuleList(new GameEnterNameConfigDaoImpl(dbParam).getFightMatchRuleList(tmpActivityId));
            nameConfig.setFightZoneList(new GameEnterNameConfigDaoImpl(dbParam).getFightZoneList(tmpActivityId));

            //config配置
            Map<String, ActivityConfig> activityConfigMap = NormalActivityMgr.getActivityConfigMap(tmpActivityId);
            if (activityConfigMap.containsKey(eActivityConfigType.AREAFIGHT_TIME.getName())) {
                List<Integer> list = StringUtils.stringToIntegerList(activityConfigMap.get(eActivityConfigType.AREAFIGHT_TIME.getName()).getValue(), "\\|");
                nameConfig.setNameTime(list.get(0));
                nameConfig.setGroupTime(list.get(1));
            }

            if (activityConfigMap.containsKey(eActivityConfigType.AREAFIGHT_MEDAL_ID.getName())) {
                nameConfig.setMedalId(activityConfigMap.get(eActivityConfigType.AREAFIGHT_MEDAL_ID.getName()).getIntValue());
            }

            if (activityConfigMap.containsKey(eActivityConfigType.AREAFIGHT_SPREAD_WEALTH_CONFIG.getName())){
                nameConfig.setDisperseGoldReward(activityConfigMap.get(eActivityConfigType.AREAFIGHT_SPREAD_WEALTH_CONFIG.getName()).getValue());
            }
            nameConfig.setSIGN_UP_LIMIT(activityConfigMap.get(eActivityConfigType.AREAFIGHT_SIGN_UP_LIMIT.getName()).getValue());
            EnterNameActivityMgr.putConfig(tmpActivityId, nameConfig);

            AreaWashePkConfig pkConfig = new AreaWashePkConfig();
            pkConfig.setScoreItemId(activityConfigMap.get(eActivityConfigType.AREAFIGHT_SCORE_ITEM_ID.getName()).getIntValue());
            pkConfig.setCartLvList(StringUtils.stringToIntegerList(activityConfigMap.get(eActivityConfigType.AREAFIGHT_CARD_USE_LIMIT.getName()).getValue(), ";"));
            pkConfig.setSceneList(StringUtils.stringToIntegerList(activityConfigMap.get(eActivityConfigType.AREAFIGHT_SCENE.getName()).getValue(), "\\|"));
            config = pkConfig;
            activityInfo = tempActivityInfo;
        }

        getLogger().info("Area Wase Pk Activity end");

        // 在线玩家下发数据
        for (GamePlayer gamePlayer : GamePlayerMgr.getAllOnlinePlayer()) {
            AreaWashePkModule module = gamePlayer.getModule(AreaWashePkModule.class);
//            module.syncConfig();
//            module.initUserData();
//            module.syncUserData();
        }
    }

    public static ActivityInfo getActivityInfo() {
        return activityInfo;
    }

    public static AreaWashePkConfig getConfig() {
        return config;
    }
}
